package com.example.aspect;
import org.aspectj.lang.ProceedingJoinPoint;

public class OrderServiceAspect {

    /* 1. 前置增强：add/delete/find */
    public void before() {
        System.out.println("hello, 我是一个前置增强");
    }

    /* 2. 后置增强：add/delete */
    public void after() {
        System.out.println("bye, 我是一个后置增强");
    }

    /* 3. 返回后增强：统一打印返回值 */
    public void afterReturning(Object ret) {
        System.out.println("方法返回值 = " + ret);
    }

    /* 4. 环绕增强：统计耗时 */
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        long s = System.nanoTime();
        Object ret = pjp.proceed();
        long e = System.nanoTime();
        System.out.println("方法执行时间 = " + (e - s) / 1_000_000 + " 毫秒");
        return ret;
    }

    /* 5. 权限检查：add/delete  */
    public void checkPermission() {
        int flag = new java.util.Random().nextInt(2); // 0 有  1 无
        if (flag == 0) {
            System.out.println("权限检查通过");
        } else {
            System.out.println("没有权限，将终止后续业务！");
            throw new RuntimeException("权限不足");
        }
    }
}